package com.example.hospital.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.example.hospital.api.common.PageUtils;
import com.example.hospital.api.common.R;
import com.example.hospital.api.controller.form.*;
import com.example.hospital.api.service.DoctorService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("doctor")
public class DoctorController {

    @Resource
    private DoctorService doctorService;

    @PostMapping("selectByPage")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:SELECT"}, mode = SaMode.OR)
    public R selectByPage(@RequestBody @Valid DoctorPageForm form) {
        Map param = BeanUtil.beanToMap(form);
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        param.put("start", start);
        PageUtils pageUtils = doctorService.selectByPage(param);
        return R.ok().put("result", pageUtils);
    }

    @PostMapping("selectContent")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:SELECT"}, mode = SaMode.OR)
    public R selectContent(@RequestBody @Valid DoctorContentForm form) {
        HashMap map = doctorService.selectContent(form.getId());
        return R.ok(map);
    }

    @PostMapping("updatePhoto")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:UPDATE"}, mode = SaMode.OR)
    public R updatePhoto(@RequestParam("file") MultipartFile file,
                         @RequestParam("doctorId") Integer doctorId) {
        doctorService.updatePhoto(file, doctorId);
        return R.ok();
    }

    @PostMapping("insert")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:INSERT"}, mode = SaMode.OR)
    public R insert(@RequestBody @Valid DoctorForm form) {
        Map param = BeanUtil.beanToMap(form);
        if (form.getTag() != null) {
            String string = JSONUtil.parseArray(form.getTag()).toString();
            param.put("tag", string);
        }
        param.put("uuid", IdUtil.simpleUUID().toUpperCase());
        int row = doctorService.insert(param);
        return R.ok("新增成功:" + row);
    }

    @PostMapping("selectById")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:SELECT"}, mode = SaMode.OR)
    public R selectById(@RequestBody @Valid DoctorByIdForm form) {
        HashMap map = doctorService.selectById(form.getId());
        return R.ok(map);
    }

    @PostMapping("update")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:UPDATE"}, mode = SaMode.OR)
    public R update(@RequestBody @Valid DoctorUpdateForm form) {
        Map param = BeanUtil.beanToMap(form);
        if (form.getTag() != null) {
            String string = JSONUtil.parseArray(form.getTag()).toString();
            param.put("tag", string);
        }
        int row = doctorService.update(param);
        return R.ok("修改成功:" + row);
    }

    @PostMapping("deleteByIds")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "DOCTOR:DELETE"}, mode = SaMode.OR)
    public R deleteByIds(@RequestBody @Valid DoctorByIdsForm form) {
        int row = doctorService.deleteByIds(form.getIds());
        return R.ok("删除成功:" + row);
    }
}
